package com.njit.mailm.core.controller;

import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;

@Controller
public class TestController {

	@RequestMapping("test")
	public String test(){
		
		return "test";
	}

	//ueditor 测试
	@RequestMapping(value = "udeditor",method = RequestMethod.GET)
	public String showUeditor(){
		return "testUeditor";
	}

	@RequestMapping(value = "upload",method = RequestMethod.GET)
	public String upload(){
		return "testUpload";
	}

	@RequestMapping(value = "upload",method = RequestMethod.POST)
	public String upload(@RequestParam("file") MultipartFile file, HttpSession session){
		// 判断文件是否为空
		if (!file.isEmpty()) {
			try {
				String basePath = session.getServletContext().getRealPath("/") + "WEB-INF/upload";
				File baseFile = new File(basePath);
				if (!baseFile.exists()){
					baseFile.mkdir();
				}
				// 文件保存路径
				String filePath = session.getServletContext().getRealPath("/") + "WEB-INF/upload/"
						+ file.getOriginalFilename();
				// 转存文件
				file.transferTo(new File(filePath));
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("附件上传失败！");
				return "error";
			}
		}
		System.out.println("附件上传成功");
		return "success";
	}

	@RequestMapping("testJsTree")
	public String testJsTree(){
		return "testJsTree";
	}
}
